package homework21;

import java.sql.*;

public class Work2_Test {

    public static void main(String[] args) throws Exception {

        //1.获取连接对象
//        String url = "jdbc:mysql://localhost:3306/task_02?characterEncoding=UTF-8";
//        Connection con = DriverManager.getConnection(url, "root", "123457");
        Connection con = DBUtils.getConnection();

        //2.获取PrepareStatement预编译对象
        //编写sql，使用？占位符方式
//            String sql = "SELECT * FROM EMPLOYEE";
        String sql = "select * from employee where name = ? and gender = ?";
        PreparedStatement ps = con.prepareStatement(sql);
//        PreparedStatement ps = con.prepareStatement("select * from employee where name = ? and gender = ?");

        //设置占位符参数
        ps.setString(1,"张三");
        ps.setString(2,"男");

        //3.根据name和gender进行
        ResultSet resultSet = ps.executeQuery();

//        System.out.println(resultSet.next());
        //4.打印查询结果
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String gender = resultSet.getString("gender");
            double salary = resultSet.getDouble("salary");
            double bonus = resultSet.getDouble("bonus");
            Date join_data = resultSet.getDate("join_date");
            System.out.println(id + " " + name + " " + gender + " " + salary + " " + bonus + " " + join_data);
        }

        //5.关闭资源
       DBUtils.close(con, ps, resultSet);
    }
}